<?php
	class RecordsAction extends Action{

		//展示页
		public function index(){
			$condition=[];
			$start=0;
			$size=10;
			$current_p=isset($_REQUEST['p'])?$_REQUEST['p']:1;

						// 查询条件
			if(isset($_REQUEST['search_content'])){
				$condition['record_name']=array('LIKE','%'.$_REQUEST['search_content'].'%');
				$condition['record_content']=array('LIKE','%'.$_REQUEST['search_content'].'%');
				$condition['record_comments']=array('LIKE','%'.$_REQUEST['search_content'].'%');
				$condition['_logic'] = 'OR';	
			}

			// print_r($current_p);exit;
			//总条数
			$count_record=D('Records')->getRecordCount($condition);
			// 页数
			$total_pages=ceil($count_record/$size);
			
			// 前页处理
			$previous_disable=true;
			$previous_p=1;
			// var_dump($current_p);
			if($current_p>1){
				$previous_disable=false;
				// var_dump($previous_disable);
				$previous_p=$current_p-1;
			}

			// 后页处理
			$next_disable=true;
			$next_p=$total_pages;
			if($current_p<$total_pages){
				$next_disable=false;
				$next_p=$current_p+1;
			}

			$start=($current_p-1)*$size;	
			// print_r($start);	
			
			

			$records=D('Records')->getRecords($condition,$start,$size);
			$this->assign('previous_disable',$previous_disable);
			$this->assign('next_disable',$next_disable);
			$this->assign('current_p',$current_p);
			$this->assign('previous_p',$previous_p);
			$this->assign('next_p',$next_p);
			$this->assign('total_pages',$total_pages);
			$this->assign('count_record',$count_record);
			$this->assign('records',$records);
			$this->assign('title','备忘录');
			$this->display('Records/index');
		}

		public function ajaxDoDel(){
			$result=array(
				'status'=>0,
				'data'=>'数据不存在',
				);

			if(!isset($_REQUEST['id'])){
				$this->ajaxReturn($result);
			}else{
				$condition['id']=$_REQUEST['id'];
			}

			if(!D('Records')->delRecord($condition)){
				$result=array(
					'status'=>0,
					'data'=>'删除失败',
					);
				$this->ajaxReturn($result);
			}

			$result=array(
					'status'=>1,
					'data'=>'删除成功',
					);
			$this->ajaxReturn($result);
		}

		public function ajaxDoAdd(){
			$result=array(
				'status'=>0,
				'data'=>'添加失败',
				);

			$data=[];

			if(!isset($_REQUEST['record_name']) || empty($_REQUEST['record_name'])||!isset($_REQUEST['record_content'])||empty($_REQUEST['record_content'])){
				$result['status']=0;
				$result['data']='数据不全';
				$this->ajaxReturn($result);
			}else{
				$data['record_name']=$_REQUEST['record_name'];
				$data['record_content']=$_REQUEST['record_content'];
				$data['record_comments']=(isset($_REQUEST['record_comments'])&&!empty($_REQUEST['record_comments']))?$_REQUEST['record_comments']:'';
				$data['record_add_time']=date("Y-m-d h:i:sa");
			}

			if(!D('Records')->addRecord($data)){
				$result['status']=0;
				$result['data']='添加失败';
				$this->ajaxReturn($result);
			}else{
				$result['status']=1;
				$result['data']='添加成功';
				$this->ajaxReturn($result);
			} 
		}
	}